<script setup lang="ts">
import { ref, reactive } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { useAuthStore } from '../../store/useAuthStore'
import type { RegisterRequest } from '../../types/auth'
import { ElMessage } from 'element-plus'
import { User, Lock, Message, Avatar } from '@element-plus/icons-vue'

const route = useRoute()
const router = useRouter()
const authStore = useAuthStore()

// 表单数据
const registerForm = reactive<RegisterRequest>({
  username: '',
  email: '',
  password: '',
  confirmPassword: '',
  agreement: false
})

// 自定义校验密码一致性
const validatePasswordMatch = (rule: any, value: string, callback: any) => {
  if (value !== registerForm.password) {
    callback(new Error('两次输入的密码不一致'))
  } else {
    callback()
  }
}

// 表单验证规则
const rules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 20, message: '用户名长度应为3-20个字符', trigger: 'blur' }
  ],
  email: [
    { required: true, message: '请输入邮箱', trigger: 'blur' },
    { type: 'email' as const, message: '请输入有效的邮箱地址', trigger: ['blur', 'change'] }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 30, message: '密码长度应为6-30个字符', trigger: 'blur' }
  ],
  confirmPassword: [
    { required: true, message: '请确认密码', trigger: 'blur' },
    { validator: validatePasswordMatch, trigger: ['blur', 'change'] }
  ],
  agreement: [
    { type: 'boolean' as const, message: '请同意用户协议', trigger: 'change' },
    { required: true, validator: (rule: any, value: boolean, callback: any) => {
      if (value) {
        callback()
      } else {
        callback(new Error('请同意用户协议和隐私政策'))
      }
    }, trigger: 'change' }
  ]
}

// 表单引用
const formRef = ref()

// 提交状态
const isSubmitting = ref(false)

// 提交注册表单
const handleSubmit = async () => {
  // 表单验证
  if (!formRef.value) return
  
  try {
    await formRef.value.validate()
    
    isSubmitting.value = true
    
    // 调用注册方法
    const success = await authStore.register(registerForm)
    
    if (success) {
      ElMessage.success('注册成功')
      // 注册成功，直接跳转到工具中心
      router.push('/tools')
    }
  } catch (error: any) {
    console.error('表单验证失败', error)
    // 显示表单验证错误
    if (error.message) {
      ElMessage.error(error.message)
    }
  } finally {
    isSubmitting.value = false
  }
}

// 前往登录页
const goToLogin = () => {
  router.push({ name: 'Login', query: route.query })
}
</script>

<template>
  <div class="register-page">
    <div class="register-container">
      <div class="register-form-wrapper">
        <div class="register-header">
          <h1 class="register-title">创建账号</h1>
          <p class="register-subtitle">完成注册后即可使用水印去除工具的所有功能</p>
        </div>
        
        <el-form
          ref="formRef"
          :model="registerForm"
          :rules="rules"
          label-position="top"
          class="register-form"
          @keyup.enter="handleSubmit"
        >
          <el-form-item prop="username">
            <el-input
              v-model="registerForm.username"
              placeholder="请输入用户名"
              size="large"
              :prefix-icon="User"
              class="custom-input"
              clearable
            />
          </el-form-item>
          
          <el-form-item prop="email">
            <el-input
              v-model="registerForm.email"
              placeholder="请输入邮箱"
              size="large"
              :prefix-icon="Message"
              class="custom-input"
              clearable
            />
          </el-form-item>
          
          <el-form-item prop="password">
            <el-input
              v-model="registerForm.password"
              type="password"
              placeholder="请输入密码"
              size="large"
              :prefix-icon="Lock"
              show-password
              class="custom-input"
              clearable
            />
          </el-form-item>
          
          <el-form-item prop="confirmPassword">
            <el-input
              v-model="registerForm.confirmPassword"
              type="password"
              placeholder="请确认密码"
              size="large"
              :prefix-icon="Lock"
              show-password
              class="custom-input"
              clearable
            />
          </el-form-item>
          
          <el-form-item prop="agreement" class="agreement-item">
            <el-checkbox v-model="registerForm.agreement">
              我已阅读并同意
              <el-button type="text" class="agreement-link">用户协议</el-button>
              和
              <el-button type="text" class="agreement-link">隐私政策</el-button>
            </el-checkbox>
          </el-form-item>
          
          <el-button
            type="primary"
            class="register-btn"
            size="large"
            :loading="isSubmitting"
            @click="handleSubmit"
          >
            注册
          </el-button>
        </el-form>
        
        <div class="register-footer">
          <div class="divider">
            <span>已有账号？</span>
          </div>
          
          <el-button
            type="default"
            class="login-btn"
            size="large"
            @click="goToLogin"
          >
            登录账号
          </el-button>
        </div>
      </div>
      
      <div class="register-banner">
        <div class="banner-content">
          <h2 class="banner-title">专业水印去除</h2>
          <p class="banner-text">注册成功后，您将获得我们的高级功能，处理更多格式和更大尺寸的图片。</p>
          <div class="banner-features">
            <div class="feature-item">
              <div class="feature-icon"><i class="el-icon-picture-outline"></i></div>
              <div class="feature-text">批量处理多张图片</div>
            </div>
            <div class="feature-item">
              <div class="feature-icon"><i class="el-icon-upload"></i></div>
              <div class="feature-text">支持更大文件</div>
            </div>
            <div class="feature-item">
              <div class="feature-icon"><i class="el-icon-star-on"></i></div>
              <div class="feature-text">获取优先处理权限</div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped>
.register-page {
  min-height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: #f8fafc;
  padding: 1rem;
}

.register-container {
  display: flex;
  width: 100%;
  max-width: 1000px;
  min-height: 600px;
  background-color: white;
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 10px 25px rgba(0, 0, 0, 0.05);
}

.register-form-wrapper {
  flex: 1;
  padding: 2.5rem;
  display: flex;
  flex-direction: column;
}

.register-header {
  margin-bottom: 2rem;
  text-align: center;
}

.register-title {
  font-size: 2rem;
  font-weight: 600;
  color: #1a202c;
  margin-bottom: 0.5rem;
}

.register-subtitle {
  color: #718096;
  font-size: 0.95rem;
}

.register-form {
  flex: 1;
}

.custom-input :deep(.el-input__wrapper) {
  padding: 0 15px;
  height: 48px;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05);
  border: 1px solid #e2e8f0;
}

.custom-input :deep(.el-input__wrapper:hover) {
  border-color: #13c2c2;
}

.custom-input :deep(.el-input__wrapper.is-focus) {
  box-shadow: 0 0 0 2px rgba(19, 194, 194, 0.2);
  border-color: #13c2c2;
}

.agreement-item {
  margin-bottom: 1.5rem;
}

.agreement-link {
  color: #13c2c2;
  padding: 0;
  font-size: 0.9rem;
  font-weight: 600;
}

.register-btn {
  width: 100%;
  height: 48px;
  font-weight: 600;
  border-radius: 6px;
  background-color: #13c2c2;
  border-color: #13c2c2;
  transition: all 0.2s;
}

.register-btn:hover {
  background-color: #36cfc9;
  border-color: #36cfc9;
  transform: translateY(-1px);
}

.register-footer {
  margin-top: 2rem;
}

.divider {
  display: flex;
  align-items: center;
  color: #a0aec0;
  margin: 1.5rem 0;
}

.divider::before,
.divider::after {
  content: '';
  flex: 1;
  border-bottom: 1px solid #e2e8f0;
}

.divider::before {
  margin-right: 1rem;
}

.divider::after {
  margin-left: 1rem;
}

.login-btn {
  width: 100%;
  height: 48px;
  font-weight: 600;
  border-radius: 6px;
}

.register-banner {
  flex: 1;
  background: linear-gradient(135deg, #13c2c2 0%, #006064 100%);
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  overflow: hidden;
}

.banner-content {
  position: relative;
  z-index: 2;
  padding: 2.5rem;
  text-align: center;
}

.banner-title {
  font-size: 2.5rem;
  font-weight: 700;
  margin-bottom: 1rem;
}

.banner-text {
  font-size: 1.1rem;
  opacity: 0.9;
  margin-bottom: 2.5rem;
  line-height: 1.6;
}

.banner-features {
  display: flex;
  flex-direction: column;
  gap: 1.5rem;
}

.feature-item {
  display: flex;
  align-items: center;
  text-align: left;
}

.feature-icon {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background-color: rgba(255, 255, 255, 0.15);
  display: flex;
  align-items: center;
  justify-content: center;
  margin-right: 1rem;
  font-size: 1.2rem;
}

.feature-text {
  font-weight: 500;
}

/* 响应式调整 */
@media (max-width: 992px) {
  .register-container {
    flex-direction: column-reverse;
    max-width: 500px;
  }
  
  .register-banner {
    display: none;
  }
  
  .register-form-wrapper {
    padding: 2rem;
  }
}

@media (max-width: 576px) {
  .register-form-wrapper {
    padding: 1.5rem;
  }
  
  .register-title {
    font-size: 1.75rem;
  }
}
</style> 